package main

import (
	"encoding/json"
	"fmt"
)

type stu struct {
	name string // 小写开头的属性为私有属性 仅仅在此包内可见
	Age  int
}

type stu1 struct {
	Name string `json:"user_name"`
	Age  int    `json:"age"`
}

func main() {
	var s1 = stu{"小红", 13}
	json1, err := json.Marshal(s1)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Println(string(json1)) // {"Age":13}

	var s2 = stu1{"小红", 13}
	json2, err1 := json.Marshal(s2)
	if err1 != nil {
		fmt.Println("error:", err1)
	}
	fmt.Println(string(json2)) // {"user_name":"小红","age":13}

	// 反序列化
	var str1 = `{"user_name":"aaa","age":23}`
	var s3 stu1
	json.Unmarshal([]byte(str1), &s3)
	fmt.Printf("%#v\n", s3) // main.stu1{Name:"aaa", Age:23}
}
